package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginAdmin;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Admin;
import com.example.speeddating.entity.Article;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.service.ArticleService;
import com.example.speeddating.tool.SensitiveTool;
import com.example.speeddating.tool.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;

/**
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private SensitiveTool sensitiveTool;

    @Autowired
    private ArticleService articleService;

    /**
     * 文章推荐
     * @param tokenEntity
     * @param pageSize
     * @param pageNum
     * @return
     */
    @TokenCheck(roles = "user")
    @GetMapping("/recommendList")
    public BaseResponse<Page> recommendList(@LoginUser TokenEntity tokenEntity, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum){
        Long userId = tokenEntity==null?null:tokenEntity.getId();
        return RespGenerator.returnOK(articleService.recommendList(pageSize, pageNum,userId));
    }

    //话题下面的文章列表
    @TokenCheck(roles = "user")
    @GetMapping("/list/{id}")
    public BaseResponse<Page> all(@LoginUser TokenEntity tokenEntity,@PathVariable Long id,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum){
        if (tokenEntity==null){
            throw new CommonException("参数错误");
        }
        return RespGenerator.returnOK(articleService.topicArticleList(pageSize, pageNum,tokenEntity.getId(),id));
    }

    /**
     * 文章详情
     * @param tokenEntity
     * @param id
     * @return
     */
    @TokenCheck(roles = "user")
    @GetMapping("/{id}")
    public BaseResponse<Article> articleDetail(@LoginUser TokenEntity tokenEntity,@PathVariable Long id){
        Long userId = tokenEntity==null?null:tokenEntity.getId();
        return RespGenerator.returnOK(articleService.selectArticleById(id,userId));
    }


    /**
     * 发布文章
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @TokenCheck(roles = "user")
    @PostMapping("/publish")
    public BaseResponse publishArticle(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("content")==null||jsonString.get("topicId")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        String content = (String) jsonString.get("content");
        if (StringTools.isEmpty(content)){
            throw  new CommonException("内容不能为空");
        }
        content = sensitiveTool.filterText(content); //过滤
        Article article = new Article();
        article.setUserId(tokenEntity.getId());
        article.setContent(content);
        article.setTopicId(Integer.valueOf(((jsonString.get("topicId").toString()))));
        article.setLocation(jsonString.get("location")==null?null:jsonString.get("location").toString());
        articleService.publishArticle(article);
        log.info(article.getId().toString());
        return RespGenerator.returnOK(article.getId());
    }

    /**
     * 获取用户发布的文章
     * @param tokenEntity
     * @param pageSize
     * @param pageNum
     * @return
     */
    @TokenCheck(roles = "user")
    @GetMapping("/publishList")
    public BaseResponse publishArticle(@LoginUser TokenEntity tokenEntity,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum){
        return RespGenerator.returnOK(articleService.topicArticleList(pageSize, pageNum,tokenEntity.getId()));
    }

    /**
     * 获取用户发布的文章
     * @param id
     * @param pageSize
     * @param pageNum
     * @return
     */
    @TokenCheck(roles = "user")
    @GetMapping("/publishList/{id}")
    public BaseResponse publishArticle(@PathVariable Long id,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum){
        return RespGenerator.returnOK(articleService.topicArticleList(pageSize, pageNum,id));
    }

    /**
     * 获取用户关注用户发布的文章
     */

    @TokenCheck(roles = "user")
    @GetMapping("/focusArticleList")
    public BaseResponse focusArticleList(@LoginUser TokenEntity tokenEntity,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum){
        return RespGenerator.returnOK(articleService.focusArticleList(pageSize, pageNum,tokenEntity.getId()));
    }


    /**
     * 删除文章
     * @param tokenEntity
     * @param id
     * @return
     */
    @TokenCheck(roles = "user")
    @DeleteMapping("/del/{id}")
    public BaseResponse deleteArticle(@LoginUser TokenEntity tokenEntity,@PathVariable long id){
        articleService.deleteArticle(id);
        return RespGenerator.returnOK("删除成功");
    }


    /**
     * 查询所有文章
     * @param admin
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/all")
    @TokenCheck(roles = "admin")
    public BaseResponse getAllArticle(@LoginAdmin Admin admin,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        Page<Article> articlePage = articleService.selectArticles(pageSize, pageNum);
        return RespGenerator.returnOK(articlePage);
    }

}
